package com.diipo.weibo.adapter;

import java.util.LinkedList;

import com.diipo.weibo.AsyncImageLoader;
import com.diipo.weibo.ConfigInfo;
import com.diipo.weibo.R;
import com.diipo.weibo.entity.HomeAll;
import com.diipo.weibo.utils.TimeUtil;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WeiboItemAdapter extends BaseAdapter{
	
	private LinkedList<HomeAll> mList = new LinkedList<HomeAll>();
	private Context mctx;
	private AsyncImageLoader asyncImageLoader = AsyncImageLoader.getInstance();
	private LayoutInflater mInflater;

	public WeiboItemAdapter(LinkedList<HomeAll> list, Context mctx) {
		super();
		this.mList = list;
		this.mctx = mctx;
		mInflater = LayoutInflater.from(mctx);
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
//		Log.e("pyb", " getCount() called ,size =  " +  mList.size());
		return mList == null ? 0 : mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		WeiboItem item = null;
		if (convertView == null) {
			item = new WeiboItem();
			
			convertView = mInflater.inflate(R.layout.weibo_item, null);
//			 convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
			item.icon = (ImageView) convertView.findViewById(R.id.weibo_item_icon);
			item.name = (TextView) convertView.findViewById(R.id.weibo_item_name);
			item.createTime = (TextView) convertView.findViewById(R.id.weibo_item_createTime);
			item.pw_type = (ImageView) convertView.findViewById(R.id.weibo_item_pw_type);
			item.content = (TextView) convertView.findViewById(R.id.weibo_item_content);
			item.content_type = (TextView) convertView.findViewById(R.id.weibo_item_content_type);
			item.image = (ImageView) convertView.findViewById(R.id.weibo_item_iamge);
			convertView.setTag(item);
		}else{
			item = (WeiboItem) convertView.getTag();
		}
		final HomeAll homeall = mList.get(position);
		String content_type = homeall.getContent().getType();
		item.name.setText(homeall.getUser().getUsername());
//		item.createTime.setText(TimeUtil.getDate(homeall.getCreated_time()));
//		Log.e("pyb", " homeall = " + homeall + " , pos = " + position);
//		Log.e("pyb", " homeall getCreated_time = " + homeall.getCreated_time());
		item.createTime.setText(TimeUtil.getDate(mList.get(position).getCreated_time()));
		if(homeall.getPw_type().equals(ConfigInfo.COMM.PW_TYPE_REPLY)){
			item.pw_type.setVisibility(View.VISIBLE);
			item.pw_type.setImageResource(R.drawable.reply_normal);
		}
		else if(homeall.getPw_type().equals(ConfigInfo.COMM.PW_TYPE_SHARE)){
			item.pw_type.setVisibility(View.VISIBLE);
			item.pw_type.setImageResource(R.drawable.share);
		}else{
			item.pw_type.setVisibility(View.GONE);
		}
//		Log.e(" pyb ", " WeiboItemAdapter getView() called ,position = " + position+ " ,content_type = " + content_type);
		String str_content_type = "";
		if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_LINK)){ //link 0
			item.content_type.setVisibility(View.VISIBLE);
			if(homeall.getContent().getUploadfile().equals("1")){
				item.content_type.setText(mctx.getString(R.string.uploadfile ));
				//str_content_type = mctx.getString(R.string.uploadfile );
			}else{
				item.content_type.setText(mctx.getString(R.string.link ));
				//str_content_type = mctx.getString(R.string.link );
			}
		}else if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_PIC)){ //picture 
			item.image.setVisibility(View.VISIBLE);
			asyncImageLoader.loadPortrait(homeall.getContent().getUrl(), item.image);
			 
			//str_content_type = mctx.getString(R.string.picture );
		}else if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_VEDIO)){ //vedio 3
			item.content_type.setVisibility(View.VISIBLE);
			item.content_type.setText(mctx.getResources().getString(R.string.vedio ));
			//str_content_type = mctx.getString(R.string.vedio );
		}else if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_RICH_HTML) ||
				content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_RICH_EMBEDDED) ||
				content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_RICH_FORM)){ //rich HTML
			item.content_type.setVisibility(View.VISIBLE);
			item.content_type.setText(mctx.getResources().getString(R.string.rich_text));
			//str_content_type = mctx.getString(R.string.rich_text );
		}else{
			item.content_type.setVisibility(View.GONE);
		}
		asyncImageLoader.loadPortrait(homeall.getUser().getProfile_picture(), item.icon);
		if(homeall.getContent().getText().length() >= 140){
			String tmp = homeall.getContent().getText().substring(0,139) + mctx.getResources().getString(R.string.check_more);
	        SpannableString ss = new SpannableString(tmp);
	        ss.setSpan(new ForegroundColorSpan(0xff4149aa),140, tmp.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//			item.content.setText(homeall.getContent().getText().substring(0,139) + mctx.getResources().getString(R.string.check_more));
	        item.content.setText(ss);
		}else{
			item.content.setText(homeall.getContent().getText());
		}
		Log.e(" pyb ", " WeiboItemAdapter getView() called ,position = " + position);
		return convertView;
	}

}
